package com.seek.springevent.config;

import com.seek.springevent.bean.DetailExecutorService;
import org.springframework.context.annotation.Bean;
import org.springframework.context.annotation.Configuration;
import org.springframework.context.event.ApplicationEventMulticaster;
import org.springframework.context.event.SimpleApplicationEventMulticaster;

import java.util.concurrent.*;

@Configuration
public class SpringEventCofiguration {

    @Bean
    public ExecutorService executorService(){
        BlockingQueue blockingQueue = new ArrayBlockingQueue(1000);
        ThreadPoolExecutor executorService = new ThreadPoolExecutor(20,100,60L, TimeUnit.SECONDS,blockingQueue);
//        ThreadPoolExecutor executorService = new DetailExecutorService(20,100,60L, TimeUnit.SECONDS,blockingQueue);
        executorService.setRejectedExecutionHandler(new ThreadPoolExecutor.CallerRunsPolicy());
        return executorService;
    }

    @Bean
    public ApplicationEventMulticaster applicationEventMulticaster(ExecutorService executorService){
        SimpleApplicationEventMulticaster applicationEventMulticaster = new SimpleApplicationEventMulticaster();
        applicationEventMulticaster.setTaskExecutor(executorService);
        return applicationEventMulticaster;
    }
}
